// alarm/index/index.js
import http from '../../http'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		pageNo: 1,
		pageSize: 30,
		alarmList: [],
		alarmType: '',
		alarmTypeList: [{
			name: '全部',
			value: ''
		}, {
			name: '离线',
			value: 1
		}, {
			name: '功率异常',
			value: 2
		}, {
			name: '白天亮灯',
			value: 3
		}, {
			name: '度目报警',
			value: 4
		}, ]
	},
	preview(e) {
		console.log(e.currentTarget.dataset.url);
		let url = e.currentTarget.dataset.url
		let urlList = this.data.alarmList.filter(item => {
			return item.alarmType == 4
		})
		urlList = urlList.map(item => {
			return item.abnormalValue
		})
		wx.previewImage({
			current: url, // 当前显示图片的链接，不填则默认为 urls 的第一张
			urls: urlList // 需要预览的图片链接列表
		})
	},
	timestampToYMDHMS(timestamp) {
		const date = new Date(timestamp);
		const year = date.getUTCFullYear();
		const month = ('0' + (date.getMonth() + 1)).slice(-2);
		const day = ('0' + date.getDate()).slice(-2);
		const hours = ('0' + date.getHours()).slice(-2);
		const minutes = ('0' + date.getMinutes()).slice(-2);
		const seconds = ('0' + date.getSeconds()).slice(-2);
		return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
	},
	gain() {
		wx.showLoading({
			title: '加载中...'
		});
		http.get({
			url: '/zg/alarm/page',
			data: {
				pageNo: this.data.pageNo,
				pageSize: this.data.pageSize,
				projectId: this.data.projectId,
				alarmType: this.data.alarmType
			},
			then: (res) => {
				console.log('故障告警', res);
				let data = res.data.data.list
				data.forEach(item => {
					item.createTime = this.timestampToYMDHMS(item.createTime)
				})
				this.setData({
					alarmList: [...this.data.alarmList, ...data]
				})
				wx.hideLoading()
			}
		})
	},
	bottom() {
		this.setData({
			pageNo: this.data.pageNo + 1
		}, () => {
			this.gain()
		})
	},
	typeChange(e) {
		console.log(e);
		this.data.alarmTypeList.forEach(item => {
			if (item.name == e.detail.title) {
				this.setData({
					alarmType: item.value,
					pageNo: 1,
					pageSize: 30,
					alarmList: []
				}, () => {
					this.gain()
				})
			}
		})
	},
	aa() {
		wx.navigateTo({
			url: `../info/info?id=10387`,
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.setData({
			projectId: options.projectId
		}, () => {
			this.gain()
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {


	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})